<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 封装一个函数 实现拷贝 数组 或者 对象*/
        var arr = [1,2,3];
        var obj={
            name:"张三",
            age:20,
            gender:"男"
        }
        function fn(arg) {
            var argcopy = Array.isArray(arg) ? [] : {};
            for (var key in arg) {
                // 使用三目运算符
                argcopy[key] = arg[key];
            }
            return argcopy;
        }
        var newarg=fn(arr);
        console.log(newarg);
        var newarg=fn(obj);
        console.log(newarg);
    </script>
</body>

</html>